home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 13 / CU Amiga Magazine's Super CD-ROM 13 (1997)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1997-08].iso / CUCD / Graphics / Ghostscript / data / gslp.ps < prev    next >
Text File  |  1997-04-12  |  15KB  |  512 lines

  1. %    Copyright (C) 1991, 1995, 1996 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % gslp.ps - format and print text
  16.  
  17. % This utility provides functionality approximately equivalent to the Unix
  18. % `enscript' program.  It prints plain text files using a single font.
  19. % It currently handles tabs and formfeeds, but not backspaces.
  20. % It only works with fixed-pitch fonts.
  21. % Standard flags implemented:
  22. %    -12BclqRr -b -f -F -L -p
  23. % Sun flags implemented:
  24. %    -T<n>    set tab width
  25. % Flags ignored:
  26. %    -GghKkmow -# -C -d -J -n -P -S -s -t -v
  27. % Flags added:
  28. %    --detect
  29. %        treats the file as PostScript if it starts with %!
  30. %    --(heading|footing)-(left|center|right) <string>
  31. %        sets the heading/footing fields; use -B first to clear
  32. %    --first-page <n>
  33. %        sets the first page to print
  34. %    --last-page <n>
  35. %        sets the last page to print
  36. %    --spacing <n>
  37. %        for double (n=2), triple (n=3), etc. spacing
  38. % Also, the string %# in a heading or footing is replaced with the page #.
  39. /PageNumberString (%#) def
  40.  
  41. /lpdict 150 dict def
  42. lpdict begin
  43.  
  44. % build iso-latin-1 version of a font
  45. /font-to-iso-latin-1 {    % <font> font-to-iso-latin-1 <font>
  46.     %% reencode for iso latin1; from the 2nd edition red book, sec 5.6.1
  47.     dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall
  48.     /Encoding ISOLatin1Encoding def currentdict end
  49.     dup /FontName get 80 string cvs (-ISOLatin1) concatstrings cvn 
  50.     exch definefont
  51. } def
  52.  
  53. /find-latin-font {    % <name> find-latin-font <font>
  54.   findfont font-to-iso-latin-1
  55. } def
  56.  
  57. % Define the initial values of the printing parameters.
  58.  
  59. /BodyFont null def        % use default
  60.   /defaultBodyFont
  61.     { /Courier find-latin-font Landscape { 7 } { 10 } ifelse scalefont } def
  62. /Columns 1 def
  63. /DetectFileType false def
  64. /Footers false def
  65. /FootingLeft () def
  66. /FootingCenter () def
  67. /FootingRight () def
  68. /Headers true def
  69. /HeadingLeft () def
  70. /HeadingCenter () def
  71. /HeadingRight (page ) PageNumberString concatstrings def
  72. /HeadingFont null def        % use default
  73.   /defaultHeadingFont
  74.     { /Courier-Bold find-latin-font 10 scalefont } def
  75. /Landscape false def
  76. /MarginBottom 36 def        % 1/2"
  77. /MarginLeft 36 def        % 1/2"
  78. /MarginRight 36 def        % 1/2"
  79. /MarginTop 36 def        % 1/2"
  80. /MaxLines 9999 def        % max lines per page
  81. /Noisy true def            % i.e., not quiet
  82. /OutFile null def        % null = write directly to device
  83. /PageFirst 1 def
  84. /PageLast 99999 def
  85. /Spacing 1 def
  86. /Tab 8 def
  87. /Truncate false def        % wrap long lines, don't truncate
  88.  
  89. % When writing to a file, we want to write out PostScript;
  90. % when writing to the printer, we want to execute it;
  91. % some commands should be executed regardless.
  92. % lpexec provides for all this.
  93.  
  94. /lpexec        % <arg1> ... <argn> </op> <n> <do_always> lpexec -
  95.  { OutFile null eq
  96.     { pop 1 add true
  97.     }
  98.     { /t exch def 1 add /n exch def cvx
  99.       n { n -1 roll dup wosp } repeat
  100.       OutFile (\n) writestring
  101.       n t
  102.     }
  103.    ifelse
  104.     { pop load exec }
  105.     { { pop } repeat }
  106.    ifelse
  107.  } def
  108.  
  109. /lpmoveto
  110.  { /moveto 2 true lpexec
  111.  } def
  112. /lpshow
  113.  { dup length 0 ne { /show 1 false lpexec } { pop } ifelse
  114.  } def
  115. /lpsetmyfont
  116.  { dup load setfont
  117.    OutFile null ne { cvx /setfont 1 false lpexec } { pop } ifelse
  118.  } def
  119.  
  120. % Define some utility procedures.
  121.  
  122. /banner        % ypos left center right
  123.  { /HFont lpsetmyfont
  124.    3 -1 roll bannerstring pop 0 4 index pwidth showline2 pop
  125.    exch bannerstring pwidth exch sub 2 div 3 index pwidth showline2 pop
  126.    bannerstring pwidth exch sub
  127.    3 -1 roll pwidth showline2 pop
  128.  } def
  129.  
  130. /bannerstring    % string -> string width
  131.   { PageNumberString search
  132.      { exch pop pindex 4 string cvs concatstrings exch concatstrings
  133.      }
  134.     if dup stringwidth pop
  135.   } def
  136.  
  137. /beginpage
  138.  { /lindex 0 def
  139.    /skipping pindex PageFirst ge pindex PageLast le and not def
  140.    /save 0 true lpexec /pagesave exch def
  141.    skipping { nulldevice   /OutFile null def } if
  142.    Headers
  143.     { lheight hdescent add
  144.       HeadingLeft HeadingCenter HeadingRight banner
  145.     } if
  146.    /BFont lpsetmyfont
  147.  } def
  148.  
  149. /endpage
  150.  { lindex 0 ne
  151.     { Footers
  152.        { topskip plength sub hdescent add
  153.          FootingLeft FootingCenter FootingRight banner
  154.        } if
  155.       /showpage 0 false lpexec
  156.     } if
  157.    pagesave /restore 0 true lpexec
  158.    /pindex pindex 1 add def
  159.  } def
  160.  
  161. /fontheight    % <font> fontheight <ascent> <height>
  162.  { gsave setfont
  163.    newpath 0 0 moveto
  164.    (|^_j) false charpath
  165.    pathbbox exch pop dup 2 index sub 4 -2 roll pop pop
  166.    grestore exch 1.25 mul exch 1.25 mul
  167.  } def
  168.  
  169. /wosp
  170.  { OutFile ( ) writestring OutFile exch write==only
  171.  } def
  172.  
  173. /outfont        % name font ->
  174.  { OutFile null ne
  175.     { exch wosp
  176.       dup /FontName get 
  177.       dup wosp OutFile ( findfont) writestring
  178.       %% reencode for iso latin1; from the 2nd edition red book, sec 5.6.1
  179.       OutFile ( 
  180.     dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall
  181.     /Encoding ISOLatin1Encoding def currentdict end
  182.       ) writestring
  183.       wosp OutFile (-ISOLatin1 exch definefont) writestring
  184.       /FontMatrix get 0 get 1000 mul round cvi wosp
  185.       OutFile ( scalefont def\n) writestring
  186.     }
  187.     { pop pop
  188.     }
  189.    ifelse
  190.  } def
  191.  
  192. /StringFF (\f) def
  193. /CharFF StringFF 0 get def
  194. /StringTAB (\t) def
  195. /CharTAB StringTAB 0 get def
  196.  
  197. /showline        % line -> leftover_line (handles \f)
  198.  {  { showline1 dup length 0 eq { exit } if
  199.       dup 0 get CharFF ne { exit } if
  200.       endpage beginpage
  201.       skip1
  202.     }
  203.    loop
  204.  } def
  205.  
  206. /showline1        % line -> leftover_line (handles page break)
  207.  { lindex llength eq { endpage beginpage } if
  208.    lindex colines idiv cowidth mul        % x
  209.    lindex colines mod 1 add lheight mul neg fascent sub    % y
  210.    1 index cowidth add
  211.    showline2
  212.    /lindex lindex 1 add def
  213.  } def
  214.  
  215. /showline2    % string x y xlimit -> leftover_string (handles tabs)
  216.  { 2 index exch 5 2 roll lpmoveto    % xinit xlimit string
  217.     { showline3 dup length 0 eq { exit } if
  218.       dup 0 get CharTAB ne { exit } if
  219.       currentpoint exch 4 index sub tabwx div
  220.     0.05 add ceiling tabwx mul 4 index add exch lpmoveto
  221.       skip1
  222.       currentpoint pop 2 index ge { exit } if
  223.     }
  224.    loop exch pop exch pop
  225.  } def
  226.  
  227. /showline3    % xlimit string -> xlimit leftover_string
  228.         % (finds line break / tab / formfeed)
  229.  { currentpoint pop 2 index exch sub
  230.      cwx div 0.1 add cvi 0 max 1 index length min
  231.    1 index 0 3 -1 roll getinterval
  232.     % look for \f or \t
  233.    StringFF search { exch pop exch pop } if
  234.    StringTAB search { exch pop exch pop } if
  235.    dup lpshow
  236.    length dup 2 index length exch sub getinterval
  237.  } def
  238.  
  239. /skip1
  240.  { dup length 1 sub 1 exch getinterval
  241.  } def
  242.  
  243. /e== {        % <object> e== - -- print an object to stderr
  244.   (%stderr) (w) file dup 3 -1 roll write==only flushfile
  245. } def
  246.  
  247. /eprint {    % <string> eprint - -- print a string to stderr
  248.   (%stderr) (w) file dup 3 -1 roll writestring flushfile
  249. } def
  250.  
  251. % The main printing procedure
  252.  
  253. /lp        % file initial_chars ->
  254.  { /lpline exch def
  255.    /lpfile exch def
  256.    /save 0 true lpexec
  257.  
  258. % Initialize the device and fonts.
  259.    /BFont
  260.      BodyFont null eq { defaultBodyFont } { BodyFont } ifelse def
  261.    /BFont BFont outfont
  262.    Headers Footers or
  263.     { /HFont
  264.     HeadingFont null eq { defaultHeadingFont } { HeadingFont } ifelse def
  265.       /HFont HFont outfont
  266.     }
  267.    if
  268.  
  269. % Get the layout parameters.
  270.    clippath
  271.    Landscape { 90 /rotate 1 true lpexec } if
  272.    BFont setfont ( ) stringwidth pop /cwx exch def
  273.      cwx Tab mul /tabwx exch def
  274.    BFont fontheight /fheight exch def /fascent exch def
  275.    Headers Footers or { HFont fontheight } { 0 0 } ifelse
  276.      /hheight exch def /hascent exch def
  277.      /hdescent hheight hascent sub def
  278.    fheight Spacing mul /lheight exch def
  279.    Headers { hheight lheight add } { 0 } ifelse
  280.      /topskip exch def
  281.    Footers { hheight lheight add } { 0 } ifelse
  282.      /botskip exch def
  283.    /pskip topskip botskip add def
  284.     % Translate the page so that (0,0) corresponds to
  285.     % the top of the topmost body line.
  286.    pathbbox
  287.      2 index sub MarginBottom MarginTop add sub /plength exch def
  288.      2 index sub MarginLeft MarginRight add sub /pwidth exch def
  289.      pwidth Columns div /cowidth exch def
  290.      exch MarginLeft add
  291.      exch MarginBottom add plength add topskip sub
  292.      /translate 2 true lpexec
  293.    plength pskip sub lheight div cvi MaxLines min
  294.      dup /colines exch def
  295.      Columns mul /llength exch def
  296.    OutFile null ne { nulldevice } if
  297.  
  298. % Print layout
  299.    Noisy
  300.     { (Page height = ) eprint llength e==
  301.       (.\n) eprint flush
  302.     } if
  303.  
  304. % Disable stack recording so we can use stopped with readline.
  305.    $error /recordstacks false put
  306.  
  307. % Initialize for the first page.
  308.    /lbuf 1000 string def
  309.    /pindex 1 def
  310.    beginpage
  311.  
  312. % Iterate through the file.
  313.    lpline
  314.     { dup length /pos exch def
  315.       lbuf exch 0 exch putinterval
  316.        { lpfile lbuf pos lbuf length pos sub getinterval readline } stopped
  317.        {    % Filled the line before a CR or EOF.
  318.          exch pop showline
  319.        }
  320.        {    % Reached CR and/or EOF first.
  321.          exch length pos add lbuf exch 0 exch getinterval
  322.      1 index { showline } if        % omit final empty line
  323.       { dup length 0 eq Truncate or { pop () exit } if
  324.         showline
  325.       }
  326.      loop
  327.      exch not { exit } if
  328.        }
  329.       ifelse
  330.     } loop
  331.    pop
  332.  
  333. % Wrap up.
  334.    endpage
  335.    /restore 0 true lpexec
  336.  
  337.  } def
  338.  
  339. end
  340.  
  341. % Usage: <file> lp
  342. %   prints <file> using the current parameter settings.
  343. % Usage: [ <arg1> ... <argn> ] lpcommand
  344. %   interprets args like a command line.
  345.  
  346. /lp { save   lpdict begin () lp end   restore } def
  347.  
  348. lpdict begin
  349.  
  350. /splitfn        % (FontNNN) -> <font>
  351.  { dup /arg exch def length
  352.     { dup 0 le { exit } if
  353.       dup 1 sub arg exch get dup 48 ge exch 59 le and not { exit } if
  354.       1 sub
  355.     } loop
  356.    arg exch 0 exch getinterval dup cvn find-latin-font
  357.    exch arg exch anchorsearch pop pop cvr scalefont
  358.  } def
  359.  
  360. % Parse the command line switches.
  361.  
  362. /doswitch    % argn ... arg1 (-?) restofswitch ->
  363.  { exch dup cvn lpdict exch known
  364.     { cvn load exec }
  365.     { exch pop (Unknown switch: ) eprint eprint (\n) eprint }
  366.    ifelse
  367.  } def
  368.  
  369. /more        % argn ... arg1 restofswitch ->
  370.  { dup length 0 ne
  371.     { (- ) dup 1 3 index 0 get put
  372.       exch dup length 1 sub 1 exch getinterval
  373.       doswitch
  374.     }
  375.     { pop
  376.     }
  377.    ifelse
  378.  } def
  379.  
  380. /-- { (--) exch concatstrings
  381.       dup cvn lpdict exch known
  382.        { cvn load exec }
  383.        { (Unknown switch: ) eprint eprint (\n) eprint }
  384.       ifelse
  385.     } def
  386. /--columns { cvi 1 max /Columns exch def } def
  387. /--detect { /DetectFileType true def } def
  388. /--first-page { cvi /PageFirst exch def } def
  389. /--footing-center { /FootingCenter exch def   /Footers true def } def
  390. /--footing-left { /FootingLeft exch def   /Footers true def } def
  391. /--footing-right { /FootingRight exch def   /Footers true def} def
  392. /--heading-center { /HeadingCenter exch def   /Headers true def } def
  393. /--heading-left { /HeadingLeft exch def   /Headers true def } def
  394. /--heading-right { /HeadingRight exch def   /Headers true def } def
  395. /--margin-bottom { cvr 72.0 mul /MarginBottom exch def } def
  396. /--margin-left { cvr 72.0 mul /MarginLeft exch def } def
  397. /--margin-right { cvr 72.0 mul /MarginRight exch def } def
  398. /--margin-top { cvr 72.0 mul /MarginTop exch def } def
  399. /--last-page { cvi /PageLast exch def } def
  400. /--spacing { cvr /Spacing exch def } def
  401.  
  402. /-# { pop } def        % ignore
  403. /-+ { -- } def
  404. (-1)cvn { /Columns 1 def   more } def
  405. (-2)cvn { /Columns 2 def   more } def
  406. /-b { /HeadingLeft exch def   /HeadingCenter () def   /HeadingRight PageNumberString def
  407.       /Headers true def
  408.       /break true def
  409.     } def
  410. /-B { /HeadingLeft () def   /HeadingCenter () def   /HeadingRight () def
  411.       /Headers false def
  412.       /FootingLeft () def   /FootingCenter () def   /FootingRight () def
  413.       /Footers false def
  414.       /break true def
  415.       more
  416.     } def
  417. /-C { pop } def        % ignore
  418. /-c { /Truncate true def   more } def
  419. /-d { pop } def        % ignore
  420. /-f { splitfn /BodyFont exch def } def
  421. /-F { splitfn /HeadingFont exch def } def
  422. /-G { more } def    % ignore
  423. /-g { more } def    % ignore
  424. /-h { more } def    % ignore
  425. /-J { pop } def        % ignore
  426. /-K { more } def    % ignore
  427. /-k { more } def    % ignore
  428. /-l { 66 -L -B } def
  429. /-L { cvi /MaxLines exch def } def
  430. /-m { more } def    % ignore
  431. /-n { pop } def        % ignore
  432. /-o { more } def    % ignore
  433. /-p { (w) file /OutFile exch def   OutFile (%!\n) writestring } def
  434. /-P { pop } def        % ignore
  435. /-q { /Noisy false def   more } def
  436. /-r { /Landscape true def   more } def
  437. /-R { /Landscape false def   more } def
  438. /-S { pop } def        % ignore
  439. /-s { pop } def        % ignore
  440. /-T { cvi /Tab exch def } def
  441. /-v { pop } def        % ignore
  442. /-w { more } def    % ignore
  443.  
  444. /lp1        % filename ->
  445.  { break not { dup /HeadingLeft exch def } if
  446.    Noisy
  447.     { (Printing ) eprint dup eprint (\n) eprint 
  448.     } if
  449.    (r) file
  450.         % If requested, check for a PostScript file.
  451.    DetectFileType
  452.     { dup 2 string readstring pop dup (%!) eq
  453.        {    % Yes, it's a PostScript file.
  454.          pop dup 80 string readline pop pop cvx exec
  455.        }
  456.        { lp
  457.        }
  458.       ifelse
  459.     }
  460.     { () lp
  461.     }
  462.    ifelse
  463.  } bind def
  464.  
  465. /lpcstring 100 string def
  466.  
  467. end
  468.  
  469. /lpcommand        % [arg1 ... argn] -> -
  470.  {    % Push the commands on the stack in reverse order
  471.    mark exch
  472.    dup length 1 sub -1 0 { 1 index exch get exch } for pop
  473.    lpdict begin
  474.    /break false def
  475.     { dup mark eq { pop exit } if
  476.       dup length 2 ge { dup 0 get (-) 0 get eq } { false } ifelse
  477.        { dup 0 2 getinterval
  478.          exch dup length 2 sub 2 exch getinterval
  479.      doswitch
  480.        }
  481.        { dup  /matched false def
  482.           { /matched true def    lp1 } lpcstring filenameforall
  483.      matched { pop } { lp1 } ifelse        % let the error happen
  484.        }
  485.       ifelse
  486.     } loop
  487.    OutFile null ne
  488.     { OutFile (%stdout) (w) file ne { OutFile closefile } if
  489.       /OutFile null def
  490.     } if
  491.    end
  492.  } def
  493.  
  494. [ shellarguments
  495.  { ] dup length 0 ne
  496.     { lpcommand
  497.     }
  498.     { (Usage: gslp [-12BclqRr] [-b<header] [-f<font>] [-F<hfont>]\n) eprint
  499.       (        [-L<lines>] [-p<outfile>] [-T<tabwidth>] [--columns <n>]\n) eprint
  500.       (        [--detect] [--first-page <page#>] [--last-page <page#>]\n) eprint
  501.       (        [--(heading|footing)-(left|right|center) <string>]\n) eprint
  502.       (        [--margin-(top|bottom|left|right) <inches>]\n) eprint
  503.       (        [--spacing <n>] file1 ... filen\n) eprint
  504.     }
  505.    ifelse
  506.  }
  507.  { pop }
  508. ifelse
  509.